package com.trkj.controller.hjj;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import com.trkj.pojo.Team;
import com.trkj.service.ServiceException;
import com.trkj.service.hjj.SchedulingService;
import com.trkj.vo.Result_AddRule_Result;
import com.trkj.pojo.Scheduling;
import com.trkj.dao.hjj.TeamDAO;
import com.trkj.mybatisdao.PbzhouqiDAOI;
import com.trkj.pojo.GerenLxl;
import com.trkj.pojo.Holiday;
import com.trkj.pojo.Pbzhouqi;
import com.trkj.pojo.Scheduling;
import com.trkj.pojo.Shift;
import com.trkj.pojo.StaffInformation;

@Controller
public class SchedulingController {
	@Autowired
	private SchedulingService schedulingService;
	@Autowired
	private TeamDAO teamdao;
	@Autowired
	private PbzhouqiDAOI pbzhouqiDAOI;
	
	//排班中心第一步
	@GetMapping("to_core_one")
	public String Scheduling(){
		return "hjj/core_one";
	}
	//
	//排班中心第二布
	@GetMapping("to_core_twe")
	public String Schedulingtwe(Long zhouqiId,Long teamId,String teamname,Model m){
		m.addAttribute("teas", zhouqiId);
		m.addAttribute("teame", teamId);
		m.addAttribute("teamename", teamname);
		List<Scheduling> attr =schedulingService.findzhouqi(zhouqiId);
		List<Integer> attr2 = new ArrayList<>();
		attr2.add(attr.get(0).getScheduEnabledate().getDate());
		m.addAttribute("name",attr.get(0).getScheduName());
		m.addAttribute("attr", attr2);
		m.addAttribute("attrday", attr.get(0).getScheduEnabledate().getDay());
		m.addAttribute("year", attr.get(0).getScheduEnabledate().getYear());
		m.addAttribute("month", attr.get(0).getScheduEnabledate().getMonth());
		return "hjj/core_twe";
			
	}
	@RequestMapping("shq_teams_paibanadds")
	@ResponseBody
	public Result_AddRule_Result teamhhh(Long planId,Long teamId,Long scheduId){
		try {
			schedulingService.hhh(planId,teamId,scheduId);
			System.out.println(teamId+scheduId);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	@RequestMapping("to_core_twe_findsall")
	@ResponseBody
	public List<Scheduling> paibanfind(Long zhouqiId /*,Long teamId*/){
		List<Scheduling> attr =schedulingService.findzhouqi(zhouqiId);
		/*List<Scheduling> attrs =schedulingService.findzhouqi(teamId);*/
		return attr;
		
	}
	//班组
	@GetMapping("to_core_team")
	public String team(){
		return "hjj/team";
		
	}
	
	//班组新增
	@GetMapping("to_core_teamadd")
	public String teamadd(){
		return "hjj/teamadd";
		
	}

	/**
	 * 排版中心清单查找
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findall_findallspaiban")
	@ResponseBody
	public Map<String,Object> paibanfindall(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findallspaiban().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findallspaiban();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}

	/**
	 * 排版中心清单查找 按照名字模糊搜索
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findall_findallspaibanbynamemh")
	@ResponseBody
	public Map<String,Object> paibanfindallbyName(int page,int rows,String teamName){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findallspaibanbyName(teamName).size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findallspaibanbyName(teamName);
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	/**
	 * 排版中心清单查找 按照名字精确搜索
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findall_findallspaibanbynamejq")
	@ResponseBody
	public Map<String,Object> paibanfindallbyNamejq(int page,int rows,String teamName){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findallspaibanbyNamejq(teamName).size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findallspaibanbyNamejq(teamName);
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	/**
	 * 班组查询所有
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findall_team")
	@ResponseBody
	public Map<String,Object> banzufindall(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findalls().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findalls();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	
	/**
	 * 班组查询所有模糊
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findall_teambyname")
	@ResponseBody
	public Map<String,Object> banzufindall(int page,int rows,String key){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findisallbyName(key).size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findisallbyName(key);
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	@RequestMapping("to_findall_teamname")
	@ResponseBody
	public List<Team> findallhos(){
		return schedulingService.finteam();
	}
	/**
	 * 班组查询所有
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findallone_team")
	@ResponseBody
	public Map<String,Object> banzufindallone(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findallsone().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findallsone();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	

	/**
	 * 班组查询所有 模糊
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("to_findallone_team_mohu")
	@ResponseBody
	public Map<String,Object> banzufindallonemh(int page,int rows,String teamName){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findallsonemh(teamName).size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findallsonemh(teamName);
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}

	//部门表查询所有
	@RequestMapping("findlaldept")
	@ResponseBody
	public Map<String,Object> deptfindall(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.deptfindall().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.deptfindall();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	//员工表查询所有
	@RequestMapping("findallemp")
	@ResponseBody
	public Map<String,Object> empfindall(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.empfindall().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.empfindall();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	
	//班组新增
	@RequestMapping("shq_team_adds")
	@ResponseBody
	public Result_AddRule_Result teamadd(Long teamId,String teamName,Timestamp teamTakeeffect,Timestamp teamLnvalid,
			String teamRemarks ,Timestamp teamFounddate,String 	teamPerson,Integer team_state,Long 	defideId,String defideName,HttpSession session){
		try {
			StaffInformation sta=(StaffInformation) session.getAttribute("staff");
			schedulingService.teamadds(teamId, teamName, teamTakeeffect, teamLnvalid, teamRemarks, teamFounddate, defideId, defideName, sta.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	//查看
	@GetMapping("to_team_updatefindall")
	public String attendancess(Long teamId,Model mod){
		Team attr =teamdao.findOne(teamId);
		mod.addAttribute("tea", attr);
		return "hjj/checking";
	}
	//确认事务中心编辑
	@GetMapping("to_team_updates")
	public String updatedancess(Long teamId,Model mod){
		Team attr =teamdao.findOne(teamId);
		mod.addAttribute("tea", attr);
		return "hjj/teamupdate";
	}
	
	//编辑
	@RequestMapping("shq_team_updates")
	@ResponseBody
	public Result_AddRule_Result Affaircategoryupdates(Long teamId,String teamName,Timestamp teamTakeeffect,Timestamp teamLnvalid,
			String teamRemarks ,Timestamp teamFounddate,String 	teamPerson,Integer team_state,Long 	defideId,String defideName){
		try {
			schedulingService.teamupdates(teamId, teamName, teamTakeeffect, teamLnvalid, teamRemarks, teamFounddate, defideId, defideName);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	//to_team_delede
	//删除
	@PostMapping("to_team_delede")
	@ResponseBody
	public Result_AddRule_Result Sales(Long teamId){
			try {
				int ca = schedulingService.Salesdele(teamId);
				if(ca==1){
					return Result_AddRule_Result.SUCCESS;
				}else{
					return Result_AddRule_Result.failure("该规则正在被引用不能删除！！！");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return Result_AddRule_Result.failure(e.getMessage());
			}
		}
	
	//排版模板
	/**
	 * 排版模板
	 */
	//排版模板
	
	@GetMapping("to_core_scheduling")
	public String Schedulingpn(){
		return "hjj/Scheduling_template";
		
	}
	//排版模板新增界面
	@GetMapping("to_core_schedulingadd")
	public String Schedulingadd(){
		return "hjj/templateadd";
		
	}
	@RequestMapping("to_scheduling_template234")
	@ResponseBody
	public Map findallsd(int page, int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.schedulingtempl().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.schedulingtempl();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
		}
	//排版新增
	@RequestMapping("shq_teampla_adds")
	@ResponseBody
	public Result_AddRule_Result templaadd(Long scheduId,String scheduName,BigDecimal cycle,BigDecimal scheduTeam,BigDecimal scheduCurrentcycle,BigDecimal scheduCurrentteam,String scheduRemarks,String scheduPerson,BigDecimal scheduState,Timestamp scheduEnabledate){
		try {
			schedulingService.teamapldds(scheduId, scheduName, cycle, scheduTeam, scheduCurrentcycle, scheduCurrentteam, scheduRemarks, scheduPerson, scheduState,scheduEnabledate);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	//to_tschedu_delede
	//删除
	@PostMapping("to_tschedu_delede")
	@ResponseBody
	public Result_AddRule_Result Saless(Long scheduId){
		try {
			boolean b = schedulingService.Salesdelee(scheduId);
			if(b==true){
				return Result_AddRule_Result.SUCCESS;
			}else{
				return Result_AddRule_Result.failure("已有记录");
			}
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
		
	}
	/**
	 * 编辑
	 * @param m
	 * @param scheduId
	 * @param scheduName
	 * @param scheduEnabledate
	 * @param scheduRemarks
	 * @return
	 */
	@RequestMapping("shq_schling_updates")
	@ResponseBody
	public Scheduling update_affir(Model m,Long scheduId,String scheduName,Timestamp scheduEnabledate,String scheduRemarks){
		System.out.println(scheduId);
		return schedulingService.deitff(scheduId);
	}
	/**
	 * 修改
	 * @param scheduId
	 * @param scheduName
	 * @param scheduEnabledate
	 * @param scheduRemarks
	 * @return
	 */
	@RequestMapping("shq_tran_schling_updates")
	@ResponseBody
	public Result_AddRule_Result up_dateuser(Long scheduId,String scheduName,Timestamp scheduEnabledate,String scheduRemarks){
		try {
			schedulingService.updateaffair(scheduId, scheduName, scheduEnabledate, scheduRemarks);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	//个人调班
	/**
	 * 个人调班
	 */
	//个人调班
	@GetMapping("to_core_personal")
	public String person(){
		return "hjj/personal_shift";
		
	}
	//个人调班新增jm
	@GetMapping("to_core_personaladd")
	public String personadd(){
		return "hjj/personaladd";
		
	}
	/**
	 * 个人调班模糊查询
	 */
	@RequestMapping("shq_personalshifts_list_query")
	@ResponseBody
	public JSONArray empListByName(String key){
		return schedulingService.queryByName(key);
	}
	/**
	 * 个人调班查询所有
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("shq_personal_findall")
	@ResponseBody
	public Map<String,Object> perfindall(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findallsper().size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findallsper();
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	
	/**
	 * 个人调班查询所有
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("shq_personal_findall_id")
	@ResponseBody
	public Map<String,Object> perfindall(int page,int rows,Integer key){
		Map<String,Object> map = new HashMap<>();
		map.put("total", schedulingService.findpersonalbyid(key).size());
		Page<JSONArray> p = PageHelper.startPage(page,rows);
		schedulingService.findpersonalbyid(key);
		List<JSONArray> json =p.getResult();
		map.put("rows", json);
		return map;
	}
	//排班模板模糊查询
	@RequestMapping("shq_paibanmoban_list_query")
	@ResponseBody
	public Map  findTimeCardById(int page,int rows,String namees){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		schedulingService.squerysByName(namees);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
 	}
	//排班模糊查询
	@RequestMapping("shq_paibans_list_query")
	@ResponseBody
	public Map  findTimeCardByIdhhh(int page,int rows,String namees){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		schedulingService.findallsteam(namees);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
 	}
		
	//个人调班新增
@RequestMapping("shq_personalse_adds")
	@ResponseBody
	public Result_AddRule_Result perlaadd(Long gerenId,Long teamId,String teamName,Long stainId,String stainName){
		try {
			schedulingService.teamaresdds(gerenId, teamId, teamName, stainId, stainName);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	//
@RequestMapping("shq_tran_personal_updates")
	@ResponseBody
	public Result_AddRule_Result personpdates(Long GEREN_ID,Long TEAM_ID,String TEAM_NAME,Long STAIN_ID,String STAIN_NAME){
		try {
			schedulingService.personupdates(GEREN_ID, TEAM_ID, TEAM_NAME, STAIN_ID, STAIN_NAME);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	//删除
	@PostMapping("to_tscheduper_delede")
	@ResponseBody
	public void Salesssss(Long GEREN_ID){
		schedulingService.Salesdeleeper(GEREN_ID);
	}
	////班组名称查询
	@RequestMapping("shq_sheetyallsbz_list_query")
	@ResponseBody
	public Map  findTimeCardByIyg(int page,int rows,String namees){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		schedulingService.findallsteambz(namees);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
 	}
	//员工名称模糊查询
	@RequestMapping("shq_sheetyallsygg_list_query")
	@ResponseBody
	public Map  findTimeCardByIdname(int page,int rows,String namees){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		schedulingService.findallsteamyg(namees);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
 	}
}
